#-*- coding: utf-8 -*-
import os
import sys
import commands
import re
import xlwt
import xlrd
import time
import re

class pick_bugs():
        
    def find_given_title_horizontal(self,title,sheet,row = 0):
        nrows = sheet.nrows
        ncols = sheet.ncols
        #uni_title = title.decode('gbk')
        for i in range(0,ncols):
            cur_cell_value = sheet.cell_value(row,i)
            if title == cur_cell_value:
                print "Found %s"%title
                return i
        return -1
            
    def copy_column(self,title,dest_sheet,source_sheet,dest_y,dest_x,src_y=0):
        nrows = source_sheet.nrows
        ncols = source_sheet.ncols
        col_qnum = self.find_given_title_horizontal(title,source_sheet,src_y)
        if col_qnum < 0 :
            print "NOT Found %s"%title
            return -1
        delta = dest_y - src_y
        for i in range(src_y,nrows):
                dest_sheet.write(delta+i, dest_x, source_sheet.cell(i,col_qnum).value)

    
    def copy_keyinfo_to_new_xls(self,source_file,dest_file):
        source_wb = xlrd.open_workbook(source_file)
        dest_wb = xlwt.Workbook()
        worksheet = dest_wb.add_sheet("mysheet1")
        
        shxrange = range(source_wb.nsheets)
        print shxrange
        
        try:
            #sh = bk.sheet_by_name("Sheet1")
            sh = source_wb.sheet_by_index(0)
        except:
            print "no sheet in %s" % file
            exit(-1)
        nrows = sh.nrows
        ncols = sh.ncols
        
        print "nrows %d, ncols %d" % (nrows,ncols)
        
        desc_str = u"简要描述"
        
        self.copy_column(u"问题单号",worksheet,sh,0,0,0)
        self.copy_column(desc_str,worksheet,sh,0,1,0)
        self.copy_column(u"问题类别",worksheet,sh,0,2,0)
        
        col_desc = self.find_given_title_horizontal(desc_str,sh)        
        if col_desc < 0 :
            print "NOT Found %s colum"%desc_str
            exit(-1)
        for i in range(0,nrows):
                value = sh.cell(i,col_desc).value
                if self.is_audio_bug(value) is True:
                    if i is not 0:
                        worksheet.write(i,3,u"YES")
                
                #bracket_list = self.find_bracket_content(value)
                #if len(bracket_list) > 0:
                #    self.write_list_to_cells_horizontal(bracket_list,worksheet,i,3)
                
        dest_wb.save(dest_file)
    def is_match_pattern1(self,desc):
        pattern = re.compile(u"(【.*?[(音频)|(Audio)|(HiFi)].*?】)|(\\[.*?[(音频)|(Audio)|(HiFi)].*?\\])")
        all = pattern.findall(desc)
        
        ret_list=[]
        k = 0
        
        if all:
            for i in range(0,len(all)):
                for j in range(0,len(all[i])):
                    if len(all[i][j]) > 0:
                        ret_list.append(all[i][j])
        if len(ret_list) > 0:
            return True
        else:
            return False
            
    def is_match_pattern2(self,desc):
        pattern = re.compile(u"(声音)|(音质)|(噪音)|(噪声)|(POP)|(无声)|(破音)|(音量)",re.I)
        all = pattern.findall(desc)
        
        ret_list=[]
        k = 0
        
        if all:
            for i in range(0,len(all)):
                for j in range(0,len(all[i])):
                    if len(all[i][j]) > 0:
                        ret_list.append(all[i][j])
        if len(ret_list) > 0:
            return True
        else:
            return False
            
    def is_audio_bug(self,desc):
        if self.is_match_pattern1(desc) is True:
            print "pattern 1"
            return 1
        elif self.is_match_pattern2(desc) is True:
            print "pattern 2"
            return 2
        else:
            print "No Pattern"
            return -1
    
    
    def write_list_to_cells_horizontal(self,list,sheet,cell_y,cell_x):
        if len(list) == 0:
            return
        
        for iterm in list:
            sheet.write(cell_y,cell_x,iterm)
            cell_x = cell_x + 1
        
    def find_bracket_content(self,string):
        pattern = re.compile(u"(【.*?】)|(\\[.*?\\])")
        
        all = pattern.findall(string)
        
        ret_list=[]
        k = 0
        
        if all:
            for i in range(0,len(all)):
                for j in range(0,len(all[i])):
                    if len(all[i][j]) > 0:
                        ret_list.append(all[i][j])
        
        return self.remove_bracket_from_list(ret_list)
    
    def remove_bracket_from_list(self,list):
        ret_list=[]
        for item in list:
            ret_list.append(re.sub(u'[\\[\\]【】]','',item))
        return ret_list            
        
if __name__ == "__main__":
    pick_bugs = pick_bugs()
    file = "targets.xls"
    #pick_bugs.test(file)
    #pick_bugs.copy_keyinfo_to_new_xls("jazz1.xls","copy.xls")
    
    #pick_bugs.find_bracket_content(u"dfsd[abc]ddd【中国】")
    
    #pick_bugs.copy_column("title","source_sheet","dest_sheet",2,3)
    pick_bugs.is_audio_bug(u"【Jazz-TL00-VN1-自动化】自动化Pop夹具使用的耳机插头无绝缘保护")
    



    

    
